<?php
class ProcessUploadImageBehavior extends CActiveRecordBehavior {
	public static $baseUploadFolder = 'uploads';
	public $attribute;
	public $thumbAttribute;
	public $nameRule = 'pk';

	public $thumbWidth;
	public $thumbHeight;
	public $imageWidth;
	public $imageHeight;
	public $imageFolder;
	public $thumbFolder = 'tn';

	public function afterDelete($event) {
		$attribute = $this->attribute;
		if (!empty($this->getOwner()->{$attribute})) {
			@unlink(
				$this->getUploadFolder() . DIRECTORY_SEPARATOR . $this->getOwner()->{$attribute});
		}
		$attribute = $this->thumbAttribute;
		if (!empty($this->getOwner()->{$attribute})) {
			@unlink($this->getThumbFolder() . DIRECTORY_SEPARATOR . $this->getOwner()->{$attribute});
		}
	}

	public function beforeSave($event) {
		if ($this->attribute) {
			$attribute = $this->attribute;
			if ($uploadImage = CUploadedFile::getInstance($this->getOwner(), $attribute)) {
				$fileName = $this->createFileName($uploadImage);
				if ($this
					->resizeAndSave($attribute, $uploadImage, $this->imageWidth,
						$this->imageHeight, $this->getUploadFolder(), $fileName)) {
					if (($uploadThumb = CUploadedFile::getInstance($this->getOwner(),
						$this->thumbAttribute))
						|| ($uploadThumb = CUploadedFile::getInstance($this->getOwner(),
							$this->attribute))) {
						$this
							->resizeAndSave($this->thumbAttribute, $uploadThumb, $this->thumbWidth,
								$this->thumbHeight, $this->getThumbFolder(), $fileName);
					}
				} else {
					$event->isValid = false;
					return;
				}
			}
		}
	}

	protected function resizeAndSave($attribute, $uploadImage, $width, $height, $folder, $fileName,
		$deleteExistFile = true) {
		$fullPath = $folder . DIRECTORY_SEPARATOR . $fileName;
		if ($width && $height) {
			$image = Yii::app()->image->load($uploadImage->getTempName());
			$image->resize($width, $height);
			if (!$image->save($fullPath, false)) {
				return false;
			}
		} else {
			$uploadImage->saveAs($fullPath, false);
		}
		//echo CVarDumper::dump($this->getOwner()->$attribute);exit;
		if ($deleteExistFile && !empty($this->getOwner()->{$attribute})) {
			@unlink($folder . DIRECTORY_SEPARATOR . $this->getOwner()->{$attribute});
		}
		$this->getOwner()->{$attribute} = $fileName;
		return true;
	}

	protected function createFileName($uploadImage) {
		$extension = $uploadImage->getExtensionName();
		if ($this->nameRule === 'pk') {
			return uniqid($this->getOwner()->getPrimaryKey() . '_') . '.' . $extension;
		}
	}

	protected function getUploadFolder() {
		$rootPath = Yii::getPathOfAlias('public');
		$uploadRootPath = $rootPath . DIRECTORY_SEPARATOR . self::$baseUploadFolder;
		if (!is_dir($uploadRootPath)) {
			mkdir($uploadRootPath);
		}
		$uploadFolder = $uploadRootPath . DIRECTORY_SEPARATOR . $this->imageFolder;
		if (!is_dir($uploadFolder)) {
			mkdir($uploadFolder);
		}
		return $uploadFolder;
	}

	protected function getThumbFolder() {
		$thumbFolder = $this->getUploadFolder() . DIRECTORY_SEPARATOR . $this->thumbFolder;
		if (!is_dir($thumbFolder)) {
			mkdir($thumbFolder);
		}
		return $thumbFolder;
	}

}
